from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from note.models import Note
from note.serializers import NoteSerializer
from note_dir.serializers import NoteDirSerializer
from note_dir.models import NoteDir


class NoteViewSet(ModelViewSet):
    """
    笔记视图集

    list:
        获取所有笔记
    create:
        新建笔记
    retrieve:
        查看笔记
    update:
        修改笔记
    destroy:
        删除笔记
    recycle:
        加入回收站
    cancel_recycling:
        取消回收
    """

    queryset = Note.objects.all()
    serializer_class = NoteSerializer

    def login(self, request):
        request.session['uid'] = 1
        return Response(status=200)

    def create(self, request, **kwargs):
        """新建笔记"""
        ser = NoteSerializer(data=request.data)
        ser.is_valid(request=request, raise_exception=True)
        ser.save()
        return Response(ser.data, status=status.HTTP_201_CREATED)

    def update(self, request, *args, **kwargs):
        """修改笔记"""
        instance = self.get_object()
        ser = NoteSerializer(instance=instance, data=request.data)
        ser.is_valid(request=request, raise_exception=True)
        ser.save()
        return Response(ser.data)

    def recycle(self, request, id):
        """笔记回收"""
        note = Note.objects.filter(id=id).first()
        if note:
            note.is_delete = True
            note.save()
            return Response(status=status.HTTP_204_NO_CONTENT)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)

    def cancel_recycling(self, request, id):
        """取消回收"""
        note = Note.objects.filter(id=id).first()
        if note:
            note.is_delete = False
            note.save()
            return Response(NoteSerializer(note).data)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)


class ReadNoteViewSet(ModelViewSet):
    """
    笔记查询视图集

    get_notes_by_dir:
        获取指定目录下的笔记
    get_notes_by_user:
        获取指定用户的笔记
    get_notes_by_curuser:
        获取当前用户的笔记
    get_notes_tree_by_user:
        获取指定用户的笔记目录树
    get_notes_tree_by_curuser:
        获取当前用户的笔记目录树
    """

    queryset = Note.objects.filter(is_delete=False)
    serializer_class = NoteSerializer

    def get_notes_by_dir(self, request, id):
        """按目录获取笔记"""
        is_public = request.query_params.get('is_public')
        dir = NoteDir.objects.filter(id=id).first()
        if dir:
            if is_public is not None:
                notes = Note.objects.filter(dir_id=id, is_delete=False, is_public=is_public)
            else:
                notes = Note.objects.filter(dir_id=id, is_delete=False)
            return Response({
                'dir': NoteDirSerializer(dir).data,
                'notes': NoteSerializer(notes, many=True).data,
            }, status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)

    def get_notes_by_user(self, request, id):
        """按用户获取笔记"""
        is_public = request.query_params.get('is_public')
        if is_public is not None:
            notes = Note.objects.filter(user_id=id, is_delete=False, is_public=is_public)
        else:
            notes = Note.objects.filter(user_id=id, is_delete=False)
        return Response(NoteSerializer(notes, many=True).data)

    def get_notes_by_curuser(self, request):
        """按当前用户获取笔记"""
        is_public = request.query_params.get('is_public')
        user_id = request.session.get('uid')
        if user_id:
            if is_public is not None:
                notes = Note.objects.filter(user_id=id, is_delete=False, is_public=is_public)
            else:
                notes = Note.objects.filter(user_id=id, is_delete=False)
            return Response(NoteSerializer(notes, many=True).data, status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)

    def get_notes_tree_by_user(self, request, id):
        """按用户获取笔记目录树"""
        is_public = request.query_params.get('is_public')
        dirs = NoteDir.objects.filter(user_id=id)
        data = []
        for dir in dirs:
            if is_public is not None:
                notes = Note.objects.filter(dir_id=dir.id, is_delete=False, is_public=is_public)
            else:
                notes = Note.objects.filter(dir_id=dir.id, is_delete=False)
            data.append({
                'dir': NoteDirSerializer(dir).data,
                'notes': NoteSerializer(notes, many=True).data
            })
        return Response(data)

    def get_notes_tree_by_curuser(self, request):
        """按当前用户获取笔记目录树"""
        is_public = request.query_params.get('is_public')
        user_id = request.session.get('uid')
        if user_id:
            dirs = NoteDir.objects.filter(user_id=user_id)
            data = []
            for dir in dirs:
                if is_public is not None:
                    notes = Note.objects.filter(dir_id=dir.id, is_delete=False, is_public=is_public)
                else:
                    notes = Note.objects.filter(dir_id=dir.id, is_delete=False)
                data.append({
                    'dir': NoteDirSerializer(dir).data,
                    'notes': NoteSerializer(notes, many=True).data
                })
            return Response(data)
        else:
            return Response(status=status.HTTP_404_NOT_FOUND)
